package logic;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import model.BirthdayEntry;

@ManagedBean(name="addEntryBean")
@SessionScoped
public class AddEntry {

	private BirthdayEntry entry = new BirthdayEntry();
	
	public String saveNewEntry(){
		Configuration configuration=new Configuration();  
		configuration.configure();  
		ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();  
		SessionFactory sf=configuration.buildSessionFactory(sr);  
		
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(entry);
		session.getTransaction().commit();
		session.close();
		System.out.println("SAVE ENTRY...");
		System.out.println(entry.getId()+" "+entry.getName()+" "+entry.getBirthday()+" "+entry.getNote());
		entry = new BirthdayEntry();
		return "";
	}

	/**
	 * @return the entry
	 */
	public BirthdayEntry getEntry() {
		return entry;
	}

	/**
	 * @param entry the entry to set
	 */
	public void setEntry(BirthdayEntry entry) {
		this.entry = entry;
	}
	
}
